﻿@{
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}

<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a>首页</a>
            <a class="p_name"></a>
            <a>
                <cite class="c_name"></cite>
            </a>
        </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
        </a>
    </div>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">


                    <div class="layui-card-body ">
                        <h1 class="x-red" style="font-size:20px;font-weight:bold;line-height:32px;margin-bottom:12px">开发人员操作页其他人员请勿操作!</h1>
                        @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "roleadd").Any())
                        {
                            <form class="layui-form layui-col-space5">
                                <div class="layui-input-inline layui-show-xs-block">
                                    <select name="parent_id" lay-verify="required">
                                        <option value=""></option>
                                        @foreach (var ca in Model.Where(c => c.parent_id == 0))
                                        {
                                            <option value="@ca.id">@ca.name</option>
                                        }
                                    </select>
                                </div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input class="layui-input" autocomplete="off" placeholder="名称" name="name" lay-verify="required">
                                </div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input class="layui-input" autocomplete="off" placeholder="规则" name="path" lay-verify="required">
                                </div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input class="layui-input" autocomplete="off" placeholder="排序" name="sork" lay-verify="number">
                                </div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input class="layui-input" autocomplete="off" placeholder="备注" name="remark" lay-verify="">
                                </div>
                                @Html.AntiForgeryToken()
                                <div class="layui-input-inline layui-show-xs-block">
                                    <button class="layui-btn" lay-submit="" lay-filter="add"><i class="layui-icon"></i>增加</button>
                                </div>
                            </form>
                        }
                    </div>

                    <hr>
                    <div class="layui-card-body scroll">
                        @Html.AntiForgeryToken()
                        <table class="layui-table layui-form">
                            <thead>
                                <tr>
                                    <th width="20">
                                        ID
                                    </th>
                                    <th>权限名称</th>
                                    <th>权限规则</th>
                                    <th>所属分类</th>
                                    <th>排序</th>
                                    <th>备注</th>
                                    <th width="250">操作</th>
                            </thead>
                            @using yiran.Areas.Admin.Models.Entity
                            @model List<sys_menu>
                            <tbody class="x-cate">
                                @{ int index = 0;}
                                @foreach (var m in Model.Where(c => c.parent_id == 0))
                                {
                                    <tr cate-id='@m.id' fid='0'>
                                        <td>
                                            @(++index)
                                        </td>
                                        <td>
                                            <div style="color:#009688;font-weight:bold">
                                                <i class="layui-icon x-show" style="cursor:pointer" status='true'>&#xe623;</i>
                                                <i class="iconfont">@Html.Raw(@m.icon)</i>&nbsp;@m.name
                                            </div>
                                        </td>
                                        <td>
                                            @m.path
                                        </td>
                                        <td>
                                        </td>
                                        <td><input @(!(ViewData["menu"] as List<sys_menu>).Where(c => c.action == "setsork").Any() ? "disabled" : "") onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" type="text" value="@m.sork" data-value="@m.sork" data-id="@m.id" onblur="setSork(this)" id="sork" name="sork" required="" lay-verify="required" autocomplete="off" class="layui-input" /></td>
                                        <td>
                                            @m.remark
                                        </td>
                                        <td class="td-manage">
                                            @if ((ViewData["menu"] as List<sys_menu>).Where(u => u.action == "roleadd").Any())
                                            {
                                                <a title="添加" onclick="xadmin.open('添加','/admin/role/add/@m.id')" href="javascript:;">
                                                    <i class="layui-icon"></i>
                                                </a>
                                            }
                                            @if ((ViewData["menu"] as List<sys_menu>).Where(u => u.action == "roledelete").Any())
                                            {
                                                <a style="margin-left:5px" title="删除" onclick="member_del(this,'@m.id')" href="javascript:;">
                                                    <i class="layui-icon">
                                                        &#xe640;
                                                    </i>
                                                </a>
                                            }
                                        </td>
                                    </tr>
                                    @foreach (var c in Model.Where(c => c.parent_id == @m.id))
                                    {
                                        <tr fid='@m.id' style="display:none">
                                            <td>
                                                @(++index)
                                            </td>
                                            <td>
                                                <div style="color:#009688;">
                                                    &nbsp;&nbsp;&nbsp;&nbsp;
                                                    ├@c.name
                                                </div>
                                            </td>
                                            <td>
                                                @c.path
                                            </td>
                                            <td>
                                                @m.name
                                            </td>
                                            <td><input @(!(ViewData["menu"] as List<sys_menu>).Where(b => b.action == "setsork").Any() ? "disabled" : "") onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" type="text" value="@c.sork" data-value="@c.sork" data-id="@c.id" onblur="setSork(this)" id="sork" name="sork" required="" lay-verify="required" autocomplete="off" class="layui-input" /></td>
                                            <td>
                                                @c.remark
                                            </td>
                                            <td class="td-manage">
                                                @*@Url.Action("Edit", "Banner", new { area = "Admin" })*@
                                                @if ((ViewData["menu"] as List<sys_menu>).Where(u => u.action == "roleedit").Any())
                                                {
                                                    <a title="编辑" onclick="xadmin.open('编辑','/admin/role/edit/@c.id')" href="javascript:;">
                                                        <i class="layui-icon">&#xe642;</i>
                                                    </a>
                                                }
                                                @if ((ViewData["menu"] as List<sys_menu>).Where(u => u.action == "roleadd").Any())
                                                {
                                                    <a style="margin-left:5px" title="添加" onclick="xadmin.open('添加','/admin/role/add/@c.id')" href="javascript:;">
                                                        <i class="layui-icon"></i>
                                                    </a>
                                                }
                                                @if ((ViewData["menu"] as List<sys_menu>).Where(u => u.action == "roledelete").Any())
                                                {
                                                    <a style="margin-left:5px" title="删除" onclick="member_del(this,'@c.id')" href="javascript:;">
                                                        <i class="layui-icon">
                                                            &#xe640;
                                                        </i>
                                                    </a>
                                                }
                                            </td>
                                        </tr>
                                        @foreach (var o in Model.Where(o => o.parent_id == @c.id))
                                        {
                                            <tr fid='@m.id' style="display:none">
                                                <td>
                                                    @(++index)
                                                </td>
                                                <td>
                                                    <div>
                                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                        ├@o.name
                                                    </div>
                                                </td>
                                                <td>
                                                    @o.path
                                                </td>
                                                <td>
                                                    @m.name
                                                </td>
                                                <td><input @(!(ViewData["menu"] as List<sys_menu>).Where(b => b.action == "setsork").Any() ? "disabled" : "") onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" type="text" value="@o.sork" data-value="@o.sork" data-id="@o.id" onblur="setSork(this)" id="sork" name="sork" required="" lay-verify="required" autocomplete="off" class="layui-input" /></td>
                                                <td>
                                                    @o.remark
                                                </td>
                                                <td class="td-manage">
                                                    @if ((ViewData["menu"] as List<sys_menu>).Where(u => u.action == "roleedit").Any())
                                                    {
                                                        <a title="编辑" onclick="xadmin.open('编辑','/admin/role/edit/@o.id')" href="javascript:;">
                                                            <i class="layui-icon">&#xe642;</i>
                                                        </a>
                                                    }
                                                    @if ((ViewData["menu"] as List<sys_menu>).Where(u => u.action == "roledelete").Any())
                                                    {
                                                        <a style="margin-left:5px" title="删除" onclick="member_del(this,'@o.id')" href="javascript:;">
                                                            <i class="layui-icon">
                                                                &#xe640;
                                                            </i>
                                                        </a>
                                                    }
                                                </td>
                                            </tr>

                                        }
                                    }

                                }
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        layui.use(['form'], function () {
            form = layui.form;
            @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "roleadd").Any()) {
                <text>
                    form.on('submit(add)', function(data) {
                        $.ajax({
                            url: '/Admin/Role/Add',
				            type: 'POST',
				            data: data.field,
				            dataType: 'json',
				            async: true,
                            success: function(data, status, xhr) {
                                    if (data.code == 200) {
                                        layer.msg(data.result, {
                                        icon: 1,
                                        time: 2000
                                        }, function () {
                                            parent.location.reload();
                                        });
                                    } else {
                                        layer.msg(data.result, { icon: 2 });
                                    }
                                },
                            error: function() {
                                    layer.msg('出错了~', { icon: 2 });
                                }
                        });
                        return false;
                    });
                </text>
            }
        });
        //排序
        @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "setsork").Any()) {
            <text>
                function setSork(obj) {
                    var sork = $(obj).val();
                    var id = $(obj).attr("data-id")
                    if (sork != $(obj).attr("data-value")) {
                    layer.confirm('是否更改排序？', function(index) {
                        var data = {
                    id: id,
                            sork: sork,
                    "__RequestVerificationToken":$(".scroll input[name=__RequestVerificationToken]").val()
                       }
                $.ajax({
                    url: '/Admin/Role/setsork',
					type: 'POST',
					data: data,
					dataType: 'json',
					async: true,
                    success: function(data, status, xhr) {
                            if (data.code == 200) {
                                layer.msg(data.result, {
                                icon: 1,
                                time: 2000
                                }, function(){
                                    location.reload();
                                });
                            } else {
                                layer.msg(data.result, { icon: 2 });
                            }
                        },
                    error: function() {
                            layer.msg("出错了~", { icon: 2 });
                        }
                    });

                }, function() {
                $(obj).val($(obj).attr("data-value"));
                });
            }
        }
        </text>
    }
        //删除
        @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "roledelete").Any()) {
        <text>
                        /*用户-删除*/
        function member_del(obj,id){
              layer.confirm('确认要删除吗？', function (index) {
                    $.ajax({
                        url:'/Admin/Role/Delete',
                        type: 'POST',
                        data: {"id[]":id,"__RequestVerificationToken":$(".scroll    input[name=__RequestVerificationToken]").val()},
					    dataType: 'json',
					    async: true,
                        success: function (data, status, xhr) {
                            if (data.code == 200) {
                                layer.msg(data.result, {
                                    icon: 1,
                                    time: 2000
                                }, function(){
                                    location.reload();
                                });
                            } else {
                                layer.msg(data.result, { icon: 2 });
                            }
                        },
                        error: function () {
                            layer.msg("出错了~", { icon: 2 });
                        }
				    });
              });
        }
        </text>
        }
         $(function(){
            $("tbody.x-cate tr[fid!='0']").hide();
            // 栏目多级显示效果
            $('.x-show').click(function () {
                if($(this).attr('status')=='true'){
                    $(this).html('&#xe625;');
                    $(this).attr('status','false');
                    cateId = $(this).parents('tr').attr('cate-id');
                    $("tbody tr[fid="+cateId+"]").show();
                } else {
                    $(this).html('&#xe623;');
                    $(this).attr('status','true');
                    cateId = $(this).parents('tr').attr('cate-id');
                    $("tbody tr[fid=" + cateId + "]").hide();
               }
            })
          })

          var cateIds = [];
          function getCateId(cateId) {
              $("tbody tr[fid="+cateId+"]").each(function(index, el) {
                  id = $(el).attr('cate-id');
                  cateIds.push(id);
                  getCateId(id);
              });
        }


    </script>
</body>
